#!/bin/bash

echo "rm *.tar"
rm *.tar
dtb_file=""
uimage_file=""
mcu_file=""
app_file=""
audio_file=""
sys_file=""
mcu_ver_file=""

sh_get_mcu_ver()
{
    cat mcu_ver.txt | grep "\."
    if [ $? -eq 0  ];then
        mcu_ver_file="mcu_ver.txt"
        mcu_ver=`cat mcu_ver.txt`
        linux_ver=`cat version.txt`
        ver=$linux_ver"_"$mcu_ver
    else
        mcu_ver=`cat mcu_ver.txt`
        mcu_ver_str=`cat version.txt | awk -F '.' '{ print $2  }'`
        sed -i "s/\.$mcu_ver_str\./\.`printf "%02d" $mcu_ver`\./g" version.txt
        ver=`cat version.txt`
    fi
}

if [ -e "uImage" ];then
    uimage_file="uImage"
fi

if [ -e "anyka_ev500.dtb" ];then
    dtb_file="anyka_ev500.dtb"
fi

if [ -e "app.img" ];then
    app_file="app.img"
fi

if [ -e "audio.img" ];then
    audio_file="audio.img"
fi

if [ -e "sys.img" ];then
    sys_file="sys.img"
fi

if [ -e "mcu.bin" ];then
    echo "rm mcu.bin"
    rm mcu.bin
fi

ver=`cat version.txt`

mcu_bin=`ls | grep bin`
if [ "$mcu_bin" != "" ];then
    echo "cp *.bin mcu.bin"
    cp *.bin mcu.bin
    mcu_file="mcu.bin"

    sh_get_mcu_ver
fi

now_time=`date +%y%m%d_%H%M`
echo "tar cf ota_$now_time"_"$ver.tar version.txt $sys_file $app_file $dtb_file $uimage_file $mcu_ver_file $mcu_file"
tar cf ota_$now_time"_"$ver.tar version.txt $sys_file $app_file $dtb_file $uimage_file $mcu_ver_file $mcu_file

